import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path';
import prismjs from 'vite-plugin-prismjs';

// https://vite.dev/config/
export default defineConfig({
    base: process.env.NODE_ENV === 'production' ? '/' : '/',
    root: process.cwd(),
    plugins: [
        vue(),
        // prismjs 配置
        prismjs({
            languages: ['javascript', 'css', 'less', 'html', 'json', 'sass', 'scss', 'markdown', 'md', 'bash', 'shell', 'ts', 'java', 'javadoc', 'php'],
            plugins: [
                'toolbar',
                'show-language',
                'copy-to-clipboard',
                'normalize-whitespace',
                'line-numbers',
                'unescaped-markup'
            ],
            theme: 'okaidia',
            css: true
        })
    ],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src'),
            '@mock': path.resolve(__dirname, './mock')
        },
    },
    // 服务端渲染
    server: {
        host: '0.0.0.0',
        port: 8082,
    },
    optimizeDeps: {
        include: ['ant-design-vue/es/locale/zh_CN', 'ant-design-vue/es/locale/en_US'],
        exclude: ['vue-demi'],
    },
    build: {
        //@ts-ignore
        brotliSize: false,
        outDir: 'dist',
        assetsDir: 'assets', // 指定生成静态文件目录
        assetsInlineLimit: 4096, // 小于此阈值的导入或引用资源将内联为 base64 编码
        sourcemap: false, // 构建后是否生成 source map 文件
        chunkSizeWarningLimit: 2000,
        minify: 'esbuild', // 指定使用哪种混淆器
        emptyOutDir: true,
        rollupOptions: {
            output: {
                // 大于2000kb就分包
                manualChunks(id) {
                    if (id.includes('node_modules')) {
                        return id.toString().split('node_modules/')[1].split('/')[0].toString();
                    }
                },
                // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
                entryFileNames: 'js/[name].[hash].js',
                // 用于命名代码拆分时创建的共享块的输出命名
                chunkFileNames: 'js/[name].[hash].js',
                // 用于输出静态资源的命名，[ext]表示文件扩展名
                assetFileNames: (assetInfo: any) => {
                    const info = assetInfo.name.split('.');
                    let extType = info[info.length - 1];
                    if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)) {
                        extType = 'media';
                    } else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {
                        extType = 'img';
                    } else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
                        extType = 'fonts';
                    }
                    return `${extType}/[name].[hash].[ext]`;
                },
            }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    api: 'modern-compiler'
                }
            }
        },
        define: {
            __INTLIFY_PROD_DEVTOOLS__: false,
            'process.env': process.env,
        },
    }
})
